package com.sks.web.action.news;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.stereotype.Controller;

import com.sks.bean.QueryResult;
import com.sks.bean.pojo.Feature;
import com.sks.service.news.FeatureService;
import com.sks.utils.SiteUrl;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.news.FeatureForm;

/***
 * 主要处理专题操作
 * @author yuanjiayuan
 *
 */

@Controller("/cms/news/featureManage")
public class FeatureManageAction extends BasicAction{

	@Resource(name = "featureServiceBean")
	private FeatureService featureService;
	
	
	/**
	 * 验证public ActionForward add(ActionMapping, ActionForm, HttpServletRequest,
	 * HttpServletResponse)方法 如果存在同名的专题名称，则返回错误信息
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void beforeadd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionMessages errors = new ActionMessages();
		FeatureForm formbean = (FeatureForm) form;
		List params = new ArrayList();
		params.add(formbean.getName());
		StringBuffer jpql = new StringBuffer("o.name = ?" + params.size());
		if (EDIT.equals(formbean.getMethod())) {
			params.add(formbean.getFeatureId());
			jpql.append(" and o.featureId <> ?" + params.size());
		}
		QueryResult<Feature> m = featureService.getScrollData(-1, -1, jpql.toString(),
				params.toArray());
		if (null != m && m.getResultlist().size() > 0) {
			// 提示信息
			errors.add("name", new ActionMessage("专题名称不能重复", false));
			request.setAttribute(ACTION, formbean.getMethod());
		}
		saveErrors(request, errors);
	}
	
	/**
	 * 跳转到添加专题页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@Permission(module="info", privilege="update")
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		request.setAttribute("feature", featureService.getScrollData().getResultlist());
		request.setAttribute(ACTION, ADD);
		return mapping.findForward(ADD);
		
	}
	
	/**
	 * 添加专题
	 */
	@Permission(module="info", privilege="update")
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {	
		FeatureForm formbean = (FeatureForm)form;
		Date date = new Date();
		formbean.setCreateTime(date);
		Feature feature = new Feature();
		copyProperties(formbean, feature);
		featureService.save(feature);
		request.setAttribute(MESSAGE, "专题添加成功");		
		request.setAttribute("urladdress", SiteUrl.readUrl("cms.feature.list"));
		return mapping.findForward(MESSAGE);
	}
	
	/***
	 * 修改界面之前验证
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	public void beforeedit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		FeatureForm formbean = (FeatureForm) form;
		this.beforeadd(mapping, formbean, request, response);
	}
	
	
	/**
	 * 编辑专题
	 */	
	@Permission(module="info", privilege="view")
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		this.saveToken(request);
		FeatureForm formbean = (FeatureForm) form;
		Feature feature = featureService.find(formbean.getFeatureId());
		copyProperties(feature, formbean);
		request.setAttribute(ACTION, EDIT);
		return mapping.findForward(EDIT);
	}
	
	/**
	 * 修改专题
	 */
	@Permission(module="info", privilege="update")
	public ActionForward edit(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
		throws Exception {
		 FeatureForm formbean = (FeatureForm) form;
		 Feature feature = featureService.find(formbean.getFeatureId());
		 feature.setName(formbean.getName());
		 feature.setVisible(formbean.getVisible());
		 feature.setDescription(formbean.getDescription());
		 featureService.update(feature);
		 request.setAttribute(MESSAGE, "修改专题成功");
		 request.setAttribute("urladdress", SiteUrl
			.readUrl("cms.feature.list"));
	return mapping.findForward(MESSAGE);
}
	 /**
	  * 删除专题
	  */
	@Permission(module="info", privilege="update")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			FeatureForm formbean = (FeatureForm) form;
			featureService.delete(formbean.getFeatureId());
			request.setAttribute(MESSAGE, "删除专题成功");
			request.setAttribute("urladdress", SiteUrl
					.readUrl("cms.feature.list"));
			return mapping.findForward(MESSAGE);
		}
	
}
